<?php
/**
 * 文章管理
 */
namespace app\index\controller;

use app\common\controller\Homebase;
use app\common\model\News as news_model;
use app\common\model\Column as column_model;

class News extends Homebase
{
    public function initialize()
    {
        parent::initialize();
        $this->column_model = new column_model();
        $this->news_model   = new news_model();
    }

    /**
     * 文章列表
     * @return mixed
     */
    public function index()
    {
        $column_id = input('param.id', 0, 'intval');

        $column = $this->column_model->getColumnInfo($column_id);
        if (empty($column)) {
            $this->error('未找到数据，即将跳转到首页', Url('/'));
        }

        $where      = ['status'=>news_model::STATUS_ISSUE, 'column_id'=>$column_id];
        $news_count = $this->news_model->where($where)->count('id');
        $news_list  = $this->news_model->where($where)->order('id desc')->paginate(10);

        $tpl = $this->_getCloumnTpl($column);
        $this->assign('column_row', $column);
        $this->assign('news_list', $news_list);
        $this->assign('colid', $column_id);
        return $this->fetch('content:'.$tpl);
    }

    public function show()
    {
        $id = input('param.id', 0, 'intval');

        $where   = ['status'=>news_model::STATUS_ISSUE, 'id'=>$id];
        $new_row = $this->news_model->where($where)->find();
        if (empty($new_row)) {
            $this->error('文章信息错误');
        }

        // 查询当前文章所属栏目信息、潜水深度的相关栏目
        $column  = $this->column_model->getColumnInfo($new_row['column_id']);
        $sublist = $this->column_model->where(array('parentid'=>1))->select();

        $this->news_model->where($where)->setInc('read_number');  // 更新阅读数量

        // 获取上一篇、下一篇内容
        $up_row   = $this->news_model->getUpDownNews($id, $new_row['column_id'], 'up');
        $down_row = $this->news_model->getUpDownNews($id, $new_row['column_id'], 'down');

        $setting = json_decode($column['setting'], true);
        $tpl     = substr($setting['show'], 0, -5);

        $this->assign('sublist', $sublist);
        $this->assign('row', $column);
        $this->assign('colid', $new_row['column_id']);
        $this->assign('new_row', $new_row);
        $this->assign('up_row', $up_row);
        $this->assign('down_row', $down_row);
        return $this->fetch('content:'.$tpl);
    }

    /**
     * 预览内容
     * @return mixed
     */
    public function preview()
    {
        $id = input('param.id', 0, 'intval');

        $where   = ['id'=>$id];
        $new_row = $this->news_model->where($where)->find();
        if (empty($new_row)) {
            $this->error('文章信息错误');
        }

        // 查询当前文章所属栏目信息、潜水深度的相关栏目
        $column  = $this->column_model->getColumnInfo($new_row['column_id']);
        $sublist = $this->column_model->where(array('parentid'=>1))->select();

        // 获取上一篇、下一篇内容
        $up_row   = $this->news_model->getUpDownNews($id, $new_row['column_id'], 'up');
        $down_row = $this->news_model->getUpDownNews($id, $new_row['column_id'], 'down');

        $setting = json_decode($column['setting'], true);
        $tpl     = substr($setting['show'], 0, -5);

        $this->assign('sublist', $sublist);
        $this->assign('row', $column);
        $this->assign('colid', $new_row['column_id']);
        $this->assign('new_row', $new_row);
        $this->assign('up_row', $up_row);
        $this->assign('down_row', $down_row);
        return $this->fetch('content:'.$tpl);
    }

    /**
     * 根据栏目内容获取栏目的模板文件
     * @param $column
     * @return bool|string
     */
    private function _getCloumnTpl($column)
    {
        if (empty($column)) {
            return false;
        }
        $tpl = '';
        $setting = json_decode($column['setting'], true);
        switch ($column['type']) {
            case column_model::TYPE_ARTICLE:
                $tpl = substr($setting['list'], 0, -5);
                break;
            case column_model::TYPE_PAGE:
                $tpl = substr($setting['page'], 0, -5);
                break;
            case column_model::TYPE_INDEX:
                $tpl = substr($setting['index'], 0, -5);
                break;
        }

        return $tpl;
    }

}
